package sapling.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sapling.dao.CostDao;
import sapling.dao.TreeDao;
import sapling.model.Tree;

import java.util.List;
import java.util.Objects;

/**
 * Created by Administrator on 2017/6/21.
 */
@Service
@Transactional
public class CostService {


    @Autowired
    CostDao costDao;
    @Autowired
    TreeDao treeDao;

    /**
     * 当前成本合适
     * @return
     */
    public double costing() {
       double costing=0;
       double sum= costDao.sum();
       long treesum=0;
       List<Tree> trees= treeDao.findAll();
       if(Objects.nonNull(trees)&&trees.size()>0){
           treesum=trees.size();
       }else {
           treesum=1;
       }
       costing=sum/treesum;
       return costing;
    }
}
